<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="有关目录和顺序文件的操作在第8.3.6节中已有介绍和举例，本节主要介绍对记录文件的读写方法。记录文件是指文件中的每个分量是一个结构的文件，如：Fox系列数据库管理系统中的DBF文件，该文件除了文件头是由记录文件的整体信息和各字段描述信息之外，文件的主体内容就是由同一个结构组成的。 下面通过二个例子来介绍记录文件的读写方法。 例10.11 假设有一个简单的学生结构类型student，其包括：学号、姓">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="10.4 文件操作程序">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2015/04/01/asm46/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="有关目录和顺序文件的操作在第8.3.6节中已有介绍和举例，本节主要介绍对记录文件的读写方法。记录文件是指文件中的每个分量是一个结构的文件，如：Fox系列数据库管理系统中的DBF文件，该文件除了文件头是由记录文件的整体信息和各字段描述信息之外，文件的主体内容就是由同一个结构组成的。 下面通过二个例子来介绍记录文件的读写方法。 例10.11 假设有一个简单的学生结构类型student，其包括：学号、姓">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-01-23T02:25:04.965Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="10.4 文件操作程序">
<meta name="twitter:description" content="有关目录和顺序文件的操作在第8.3.6节中已有介绍和举例，本节主要介绍对记录文件的读写方法。记录文件是指文件中的每个分量是一个结构的文件，如：Fox系列数据库管理系统中的DBF文件，该文件除了文件头是由记录文件的整体信息和各字段描述信息之外，文件的主体内容就是由同一个结构组成的。 下面通过二个例子来介绍记录文件的读写方法。 例10.11 假设有一个简单的学生结构类型student，其包括：学号、姓">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2015/04/01/asm46/">





  <title>10.4 文件操作程序 | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2015/04/01/asm46/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">10.4 文件操作程序</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2015-04-01T20:36:11+08:00">
                2015-04-01
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/汇编教程/" itemprop="url" rel="index">
                    <span itemprop="name">汇编教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>有关目录和顺序文件的操作在第8.3.6节中已有介绍和举例，本节主要介绍对记录文件的读写方法。记录文件是指文件中的每个分量是一个结构的文件，如：Fox系列数据库管理系统中的DBF文件，该文件除了文件头是由记录文件的整体信息和各字段描述信息之外，文件的主体内容就是由同一个结构组成的。</p>
<p>下面通过二个例子来介绍记录文件的读写方法。</p>
<p><strong>例10.11 假设有一个简单的学生结构类型student，其包括：学号、姓名和年龄等信息，要求编写一个程序，该程序接受从键盘输入的学生记录信息，并把它们保存在文件students.dat之中。</strong></p>
<p>解：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br></pre></td><td class="code"><pre><span class="line">.MODEL        SMALL, C</span><br><span class="line"></span><br><span class="line">.486</span><br><span class="line"></span><br><span class="line">     student  STRUCT</span><br><span class="line"></span><br><span class="line">          id  DW        ?</span><br><span class="line"></span><br><span class="line">       sname  DB        10 DUP(?)</span><br><span class="line"></span><br><span class="line">         age  DB        ?</span><br><span class="line"></span><br><span class="line">     student  ENDS</span><br><span class="line"></span><br><span class="line">.DATA</span><br><span class="line"></span><br><span class="line">fname DB　&quot;Students.dat&quot;,0</span><br><span class="line"></span><br><span class="line">msg1 DB　&quot;Id:$&quot;</span><br><span class="line"></span><br><span class="line">msg2 DB　&quot;Name:$&quot;</span><br><span class="line"></span><br><span class="line">msg3 DB　&quot;Age:$&quot;</span><br><span class="line"></span><br><span class="line">msg4 DB　&quot;Continue?$&quot;</span><br><span class="line"></span><br><span class="line">msg5 DB　&quot;Fail to create file$&quot;</span><br><span class="line"></span><br><span class="line">CRLF DB　0AH, 0DH, &quot;$&quot;</span><br><span class="line"></span><br><span class="line">buff DB　?, ?, 11 DUP(?)</span><br><span class="line"></span><br><span class="line">peason STUDENT &lt;&gt;</span><br><span class="line"></span><br><span class="line">.CODE</span><br><span class="line"></span><br><span class="line">     DispMsg  PROC      USES AX DX, Msg:PTR BYTE            ;显示字符串Msg</span><br><span class="line"></span><br><span class="line">…… ;参见例10.7</span><br><span class="line"></span><br><span class="line">     DispMsg  ENDP</span><br><span class="line"></span><br><span class="line">;程序功能：把字符串Data转化成数值，不考虑负数。当遇到非法字符时，则结束转换操作； </span><br><span class="line"></span><br><span class="line">;入口参数：Data为字符串的首地址，Len为该字符串的长度； </span><br><span class="line"></span><br><span class="line">;出口参数：数值存放在AX中。 </span><br><span class="line"></span><br><span class="line">     GetData  PROC      USES BX CX SI, Len:BYTE, Data:PTR BYPE</span><br><span class="line"></span><br><span class="line">              XOR       CX, CX</span><br><span class="line"></span><br><span class="line">              MOV       CL, Len</span><br><span class="line"></span><br><span class="line">              MOV       SI, Data</span><br><span class="line"></span><br><span class="line">              XOR       AX, AX</span><br><span class="line"></span><br><span class="line">              XOR       BX, BX</span><br><span class="line"></span><br><span class="line">.REPEAT</span><br><span class="line"></span><br><span class="line">              MOV       BL, [SI]</span><br><span class="line"></span><br><span class="line">              SUB       BL, &apos;0&apos;</span><br><span class="line"></span><br><span class="line">.BREAK        .IF BL&lt;0 || BL&gt;9                            ;判断当前数值是否在0~9之间</span><br><span class="line"></span><br><span class="line">              IMUL      AX, 10</span><br><span class="line"></span><br><span class="line">              ADD       AX, BX</span><br><span class="line"></span><br><span class="line">              INC       SI</span><br><span class="line"></span><br><span class="line">.UNTILCXZ</span><br><span class="line"></span><br><span class="line">              RET</span><br><span class="line"></span><br><span class="line">     GetData  ENDP</span><br><span class="line"></span><br><span class="line">;程序功能：读取指定长度的字符串，在输入前，显示有关输入内容的提示信息； </span><br><span class="line"></span><br><span class="line">;入口参数：读入字符串的长度为Len，提示信息的首地址为MSG； </span><br><span class="line"></span><br><span class="line">;出口参数：读入信息(字符串)存放缓冲区buff中。 </span><br><span class="line"></span><br><span class="line">     GetInfo  PROC      USES AX DX, Len:BYTE, Msg:PTR BYTE</span><br><span class="line"></span><br><span class="line">              INVOKE    DispMsg, Msg            ;显示提示信息</span><br><span class="line"></span><br><span class="line">              MOV       AL, Len</span><br><span class="line"></span><br><span class="line">              MOV       buff, AL</span><br><span class="line"></span><br><span class="line">              MOV       AH, 0AH</span><br><span class="line"></span><br><span class="line">              LEA       DX, buff</span><br><span class="line"></span><br><span class="line">              INT       21H</span><br><span class="line"></span><br><span class="line">              INVOKE    DispMsg, ADDR CRLF      ;显示回车、换行</span><br><span class="line"></span><br><span class="line">              RET</span><br><span class="line"></span><br><span class="line">     GetInfo  ENDP</span><br><span class="line"></span><br><span class="line">.STARTUP</span><br><span class="line"></span><br><span class="line">              MOV       AX, DS</span><br><span class="line"></span><br><span class="line">              MOV       ES, AX</span><br><span class="line"></span><br><span class="line">              LEA       DX, fname</span><br><span class="line"></span><br><span class="line">              MOV       CX, 20H</span><br><span class="line"></span><br><span class="line">              MOV       AH, 3CH</span><br><span class="line"></span><br><span class="line">              INT       21H         ;创建文件</span><br><span class="line"></span><br><span class="line">.IF           CARRY       ;若创建失败，则显示失败信息</span><br><span class="line"></span><br><span class="line">INVOKE　DispMsg, ADDR msg5</span><br><span class="line"></span><br><span class="line">              JMP　over</span><br><span class="line"></span><br><span class="line">.ENDIF</span><br><span class="line"></span><br><span class="line">              MOV       BX, AX      ;把句柄存入BX，为后面使用作准备</span><br><span class="line"></span><br><span class="line">      again:</span><br><span class="line"></span><br><span class="line">              INVOKE    DispMsg, ADDR CRLF      ;显示回车、换行</span><br><span class="line"></span><br><span class="line">              INVOKE    GetInfo, 5, ADDR msg1   ;读取学号(假定学号为4位整数)</span><br><span class="line"></span><br><span class="line">              INVOKE    GetData, 4, ADDR buff+2 ;把学号字符串转化成数值</span><br><span class="line"></span><br><span class="line">              MOV       peason.id, AX           ;把数值型学号存入学号字段</span><br><span class="line"></span><br><span class="line">              INVOKE    GetInfo, 11, ADDR msg2  ;读取姓名(假定姓名为10个字符)</span><br><span class="line"></span><br><span class="line">              MOV       CX, 10</span><br><span class="line"></span><br><span class="line">              MOV       AL, &apos; &apos;</span><br><span class="line"></span><br><span class="line">              LEA       DI, peason.sname</span><br><span class="line"></span><br><span class="line">              REP       STOSB       ;先置姓名字段为10个空格</span><br><span class="line"></span><br><span class="line">              MOV       CL, buff+1</span><br><span class="line"></span><br><span class="line">              MOV       SI, OFFSET buff+2</span><br><span class="line"></span><br><span class="line">              LEA       DI, peason.sname</span><br><span class="line"></span><br><span class="line">              REP       MOVSB?      ;把输入的姓名存入姓名字段</span><br><span class="line"></span><br><span class="line">              INVOKE    GetInfo, 3, ADDR msg3   ;读取年龄(假定年龄为2位整数)?</span><br><span class="line"></span><br><span class="line">              INVOKE    GetData, 2, ADDR buff+2 ;把年龄字符串转化成数值</span><br><span class="line"></span><br><span class="line">              MOV       peason.age, AL          ;把数值型年龄存入年龄字段</span><br><span class="line"></span><br><span class="line">              MOV       CX, SIZE peason</span><br><span class="line"></span><br><span class="line">              LEA       DX, peason</span><br><span class="line"></span><br><span class="line">              MOV       AH, 40H</span><br><span class="line"></span><br><span class="line">              INT       21H         ;把学生记录写入文件</span><br><span class="line"></span><br><span class="line">              INVOKE    DispMsg, ADDR msg4?     ;提示是否继续输入</span><br><span class="line"></span><br><span class="line">              MOV       AH, 1</span><br><span class="line"></span><br><span class="line">              INT       21H</span><br><span class="line"></span><br><span class="line">              AND       AL, 0DFH</span><br><span class="line"></span><br><span class="line">              CMP       AL, &apos;Y&apos;?</span><br><span class="line"></span><br><span class="line">              JZ        again       ;若按y或Y，则继续输入</span><br><span class="line"></span><br><span class="line">              MOV       AH, 3EH</span><br><span class="line"></span><br><span class="line">              INT       21H</span><br><span class="line"></span><br><span class="line">       over:</span><br><span class="line"></span><br><span class="line">.EXIT         0</span><br><span class="line"></span><br><span class="line">              END</span><br></pre></td></tr></table></figure>
<p><strong>例10.12 编写一个程序显示由例10.11建立的记录文件students.dat中的学生信息。 </strong></p>
<p>解：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br></pre></td><td class="code"><pre><span class="line">.MODEL　SMALL,C</span><br><span class="line"></span><br><span class="line">     student  STRUCT</span><br><span class="line"></span><br><span class="line">id DW　?</span><br><span class="line"></span><br><span class="line">sname DB　10 DUP(?)</span><br><span class="line"></span><br><span class="line">age DB　?</span><br><span class="line"></span><br><span class="line">     student  ENDS</span><br><span class="line"></span><br><span class="line">.DATA</span><br><span class="line"></span><br><span class="line">fname DB　&quot;Students.dat&quot;,0</span><br><span class="line"></span><br><span class="line">id1 DB　&quot;Id:&quot;, 4 dup(?), 0dh, 0ah, &quot;$&quot;</span><br><span class="line"></span><br><span class="line">name1 DB　&quot;Name:&quot;, 10 dup(&apos; &apos;), 0dh, 0ah, &quot;$&quot;</span><br><span class="line"></span><br><span class="line">age1 DB　&quot;Age:&quot;, 2 dup(?), 0dh, 0ah, &quot;$&quot;</span><br><span class="line"></span><br><span class="line">msg1 DB　&quot;Fail to open file$&quot;</span><br><span class="line"></span><br><span class="line">peason student　&lt;&gt;</span><br><span class="line"></span><br><span class="line">.CODE</span><br><span class="line"></span><br><span class="line">     DispMsg  PROC      USES AX DX, Msg:PTR BYTE            ;显示字符串Msg</span><br><span class="line"></span><br><span class="line">…… ;参见例10.7</span><br><span class="line"></span><br><span class="line">     DispMsg  ENDP</span><br><span class="line"></span><br><span class="line">;程序功能：把数据Data转换成长度为Len的字符串； </span><br><span class="line"></span><br><span class="line">;入口参数：待转换数据Data，转换成字符串的长度为Len，存放字符串的首地址为PStr； </span><br><span class="line"></span><br><span class="line">;出口参数：读入信息(字符串)存放缓冲区buff中。 </span><br><span class="line"></span><br><span class="line">      GetStr  PROC      USES AX CX DX DI, Data:WORD, Len:WORD, PStr:PTR BYTE</span><br><span class="line"></span><br><span class="line">              MOV       CX, Len</span><br><span class="line"></span><br><span class="line">              MOV       DI, PStr</span><br><span class="line"></span><br><span class="line">              MOV       AL, &apos; &apos;</span><br><span class="line"></span><br><span class="line">              REP       STOSB       ;把存放字符串的缓冲区填充为空格</span><br><span class="line"></span><br><span class="line">              MOV       DI, PStr</span><br><span class="line"></span><br><span class="line">              ADD       DI, Len</span><br><span class="line"></span><br><span class="line">              DEC       DI          ;确定最后一个字符在缓冲区中的位置</span><br><span class="line"></span><br><span class="line">              MOV       AX, Data</span><br><span class="line"></span><br><span class="line">              MOV       CX, 10</span><br><span class="line"></span><br><span class="line">.REPEAT</span><br><span class="line"></span><br><span class="line">              XOR       DX, DX</span><br><span class="line"></span><br><span class="line">              IDIV      CX?         ;除10，从低位向高位求得每一位</span><br><span class="line"></span><br><span class="line">              ADD       DL, &apos;0&apos;     ;把余数转变成字符，然后存放目标单元</span><br><span class="line"></span><br><span class="line">              MOV       [DI], DL</span><br><span class="line"></span><br><span class="line">              DEC       DI</span><br><span class="line"></span><br><span class="line">.UNTIL　AX==0</span><br><span class="line"></span><br><span class="line">              RET</span><br><span class="line"></span><br><span class="line">      GetStr  ENDP</span><br><span class="line"></span><br><span class="line">.STARTUP</span><br><span class="line"></span><br><span class="line">              MOV       AX, DS</span><br><span class="line"></span><br><span class="line">              MOV       ES, AX</span><br><span class="line"></span><br><span class="line">LEA?DX, fname</span><br><span class="line"></span><br><span class="line">              MOV       AL, 0H</span><br><span class="line"></span><br><span class="line">              MOV       AH, 3DH</span><br><span class="line"></span><br><span class="line">              INT       21H         ;以“只读”方式打开指定的文件</span><br><span class="line"></span><br><span class="line">.IF　CARRY? ;若创建失败，则显示失败信息 </span><br><span class="line"></span><br><span class="line">INVOKE　DispMsg, ADDR msg1</span><br><span class="line"></span><br><span class="line">              JMP　over</span><br><span class="line"></span><br><span class="line">.ENDIF</span><br><span class="line"></span><br><span class="line">              MOV       BX, AX      ;把句柄存入BX，为后面使用作准备</span><br><span class="line"></span><br><span class="line">      again:</span><br><span class="line"></span><br><span class="line">              MOV       CX, SIZE peason</span><br><span class="line"></span><br><span class="line">              LEA       DX, peason</span><br><span class="line"></span><br><span class="line">              MOV       AH, 3FH</span><br><span class="line"></span><br><span class="line">              INT       21H         ;从文件中读出一个记录</span><br><span class="line"></span><br><span class="line">.IF　CARRY? || AX==0 ;若读记录出错或遇到文件尾，结束 </span><br><span class="line"></span><br><span class="line">              JMP       close</span><br><span class="line"></span><br><span class="line">.ENDIF</span><br><span class="line"></span><br><span class="line">              INVOKE    GetStr, peason.id, 4, ADDR Id1+3    ;把“学号”转换成字符串</span><br><span class="line"></span><br><span class="line">              INVOKE    DispMsg, ADDR Id1       ;显示“学号”字符串</span><br><span class="line"></span><br><span class="line">              MOV       CX, 10</span><br><span class="line"></span><br><span class="line">              LEA       SI, peason.sname</span><br><span class="line"></span><br><span class="line">              LEA       DI, Name1+5</span><br><span class="line"></span><br><span class="line">              REP       MOVSB?      ;把“姓名”转移到显示区</span><br><span class="line"></span><br><span class="line">              INVOKE    DispMsg, ADDR Name1     ;显示“姓名”字符串</span><br><span class="line"></span><br><span class="line">              INVOKE    Getstr, peason.age, 2, ADDR Age1+4  ;把“年龄”转换成字符串</span><br><span class="line"></span><br><span class="line">              INVOKE    Dispmsg, ADDR Age1      ;显示“年龄”字符串</span><br><span class="line"></span><br><span class="line">              JMP       again</span><br><span class="line"></span><br><span class="line">      close:</span><br><span class="line"></span><br><span class="line">              MOV       AH, 3EH</span><br><span class="line"></span><br><span class="line">              INT       21H         ;关闭当前打开的文件</span><br><span class="line"></span><br><span class="line">       over:</span><br><span class="line"></span><br><span class="line">.EXIT         0</span><br><span class="line"></span><br><span class="line">              END</span><br></pre></td></tr></table></figure>
<p>从例10.11和10.12，我们不难掌握记录文件的读写方法。有兴趣的读者，还可以利用文件指针的定位来指定读写某个具体的记录。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2015/04/01/asm45/" rel="next" title="10.3 数据转换程序">
                <i class="fa fa-chevron-left"></i> 10.3 数据转换程序
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2015/04/01/asm47/" rel="prev" title="10.5 动态数据的编程">
                10.5 动态数据的编程 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
